﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Operator_TransferClients : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        int operatorId = -1;
        if (Session["operatorId"] != null)
        {
            operatorId = Convert.ToInt32(Session["operatorId"].ToString());

            var profiles = from profile in db.UserProfiles where profile.OperatotId == operatorId select profile;
            if (profiles.Count() == 0)
            {
                deleteOperator();
            }
        }
        Response.Redirect("~/Admin/Operators.aspx");
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            int newOperatorId = Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex]["OperatorId"].ToString());
            int operatorId = -1;
            if (Session["operatorId"] != null)
            {
                operatorId = Convert.ToInt32(Session["operatorId"].ToString());
            }
            var profiles = from profile in db.UserProfiles where profile.OperatotId == operatorId select profile;
            foreach (UserProfile userProfile in profiles)
            {
                userProfile.OperatotId = newOperatorId;
            }
            db.SubmitChanges();
            deleteOperator();
            Response.Redirect("~/Admin/Operators.aspx");
        }
        catch
        {
        }
    }

    protected void deleteOperator()
    {
        int operatorId = -1;
        if (Session["operatorId"] != null)
        {
            operatorId = Convert.ToInt32(Session["operatorId"].ToString());
        }
        var bankOperators = from bankOperator in db.Operators where bankOperator.OperatorId == operatorId select bankOperator;
        Operator delOperator = bankOperators.First();
        string operatorUserName = delOperator.aspnet_User.UserName;
        db.Operators.DeleteOnSubmit(delOperator);
        db.SubmitChanges();
        Membership.DeleteUser(operatorUserName);
    }
}